﻿using System.Xml.Linq;
using System.Globalization;
using System;
using System.Runtime.Serialization;

namespace TVersityDotNet
{
    [DataContractAttribute]
    public class TVersityResponse
    {
        [DataMember]
        public virtual bool ResponseSuccess { get; set; }
        [DataMember]
        public virtual string ResponseMessage { get; set; }

        /// <summary>
        /// The default expected return format for data
        /// Default is didlite
        /// </summary>
        public static string DefaultFormat = "didlite";
        
        public virtual XDocument ResponseXml { get; set; }

        public virtual void ParseMessage(XDocument responseXml)
        {
            if (responseXml != null)
            {
                this.ResponseXml = responseXml;
                XElement xElem = responseXml.Element("response");
                string statusString = xElem.Attribute("status").Value;
                this.ResponseSuccess = string.Compare(statusString, "success", StringComparison.OrdinalIgnoreCase) == 0;
                this.ResponseMessage = xElem.Attribute("message").Value;
            }
            else
            {
                this.ResponseSuccess = false;
                this.ResponseMessage = "Provided responseXml was null";
            }
        }
    }
}
